#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>

#define INF 1000000000
#define MAXM 3010	 // 点
#define MAXN 9000100 // 边

using namespace std;

vector<int> mst[MAXM];

struct edge
{
	int u, v, c;
	bool operator<(const edge &rhs) const
	{
		return c < rhs.c;
	}
} edges[MAXN];

int nCount = 0; // total edges
int num[MAXM][MAXM], best[MAXM][MAXM];
int f[MAXM];
int inTree[MAXM][MAXM];

int min(int a, int b)
{
	if (a < b)
		return a;
	return b;
}

int findSet(int x)
{
	if (f[x] == x)
		return x;
	return f[x] = findSet(f[x]);
}

void Union(int a, int b)
{
	f[a] = b;
}

double Kruscal(int n, int m)
{
	int i, j;
	double ans = 0;
	sort(edges, edges + m);
	for (i = 0; i < m; i++)
	{
		int t1 = findSet(edges[i].u);
		int t2 = findSet(edges[i].v);
		if (t1 != t2)
		{
			Union(t1, t2);
			ans += edges[i].c;
			mst[edges[i].u].push_back(edges[i].v);
			mst[edges[i].v].push_back(edges[i].u);
			inTree[edges[i].u][edges[i].v] = 1;
			inTree[edges[i].v][edges[i].u] = 1;
		}
	}
	return ans;
}

int dfs(int rt, int u, int fa)
{
	int i, j, s = INF;
	for (i = 0; i < mst[u].size(); i++) // 对u结点下的儿子i遍历
	{
		int v = mst[u][i]; // 当前结点u儿子为v
		if (v == fa)
			continue;
		int tmp = dfs(rt, v, u);
		s = min(s, tmp);
		best[u][v] = best[v][u] = min(best[u][v], tmp);
	}
	if (rt != fa) // 不是
		s = min(num[rt][u], s);
	return s;
}

int main()
{
	int i, j;
	int N, M, X, Y, C, Q;
	while (1)
	{
		double ANS = 0;
		for (i = 0; i < MAXM; i++)
		{
			mst[i].clear();
			f[i] = i;
			for (j = 0; j < MAXM; j++)
			{
				num[i][j] = best[i][j] = INF;
				inTree[i][j] = 0;
			}
		}
		scanf("%d%d", &N, &M);
		if (N == 0 && M == 0)
			return 0;
		for (i = 0; i < M; i++)
		{
			scanf("%d%d%d", &edges[i].u, &edges[i].v, &edges[i].c);
			num[edges[i].u][edges[i].v] = num[edges[i].v][edges[i].u] = edges[i].c;
		}
		double minDis = Kruscal(N, M);
		for (i = 0; i < N; i++)
			dfs(i, i, -1);
		scanf("%d", &Q);
		for (i = 1; i <= Q; i++)
		{
			scanf("%d%d%d", &X, &Y, &C);
			if (!inTree[X][Y])
				ANS += minDis; // 该边不在树上，最小生成树不变
			else			   // 否则,最小生成树上的边X<->Y选择改成另一最短的边或边权为C的新边
			{
				ANS += minDis - num[X][Y] + min(best[X][Y], C);
			}
		}
		printf("%.4lf\n", ANS / (Q * 1.0));
	}
	return 0;
}